<template>
    <div>
        <div ref="refChart" style="width: 100%;height:15rem;">
        </div>
    </div>
</template>
  
<script setup lang="ts">
import { RequestStatisticGetFittingByBrand } from "@/api/path/index.js";
import {onUnmounted, onMounted, ref } from 'vue';
import * as echarts from 'echarts';
type EChartsOption = echarts.EChartsOption;
const refChart = ref();
const chartInstance = ref();
const chartData = ref();

// 获取接口数据
const getData = () => {
  return new Promise((resolve, reject) => {
    RequestStatisticGetFittingByBrand()
      .then((res: any) => {
        chartData.value = res.resultObj;
        resolve(res.resultObj);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
};
onMounted(async () => {
  await getData();
  // 图表初始化
  initChart();
  // 调用Echarts图表自适应方法
  screenAdapter();
  // Echarts图表自适应
  window.addEventListener('resize', screenAdapter);
});

const initChart = () => {
  chartInstance.value =echarts.init(refChart.value)
  const initOption: EChartsOption = {
    grid: {
      left: '0%',
      right: '0%',
      bottom: '0%',
      top: '0%',
      containLabel: true,
    },
    legend: {
      orient: 'vertical',
      right: '10%',
      top: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    color: [
      '#4c9eff',
      '#a4d0ff',
      '#01d8b4',
      '#80a3ff',
      '#2993f8',
      '#2b90f5',
      '#284cfa',
      '#192e9b',
      '#017eff'
    ],
    series: [
      {
        name: '溯源件统计',
        type: 'pie',
        radius: [10, 90],
        center: ['15%', '50%'],
        roseType: 'area',
        label: {
          show: true,
          position: 'center'
        },
        emphasis: {
          label: {

          }
        },
        labelLine: {
          show: false
        },
        data: chartData.value
      }
    ]
  };
  // 图表初始化配置
  chartInstance.value?.setOption(initOption);
};
const screenAdapter = () => {
  // 手动的调用图表对象的resize 才能产生效果
  chartInstance.value.resize({width:'auto', animation: { duration: 300 } });
};
onUnmounted(() => {
  // 销毁Echarts图表
  chartInstance.value.dispose();
  chartInstance.value = null;
  // Echarts图表自适应销毁
  window.removeEventListener('resize', screenAdapter);
});
</script>
<style lang="scss" scoped></style>